package com.dragon.www.haier.popupwindow;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.dragon.www.haier.R;
import com.dragon.www.haier.activity.MainActivity;
import com.dragon.www.haier.adapter.LampAdapter;
import com.dragon.www.haier.entity.DeviceInfo;
import com.dragon.www.haier.net.NetHelper;
import com.dragon.www.haier.net.NetUtils;

/**
 * Created by 51268 on 2016/3/13.
 */
public class LampPopWindow extends BasePopupWindow {
    private ImageView imageBackLamp;
    private final static int lampPopBackgroundColor = 0xD9FF7200;

    private ImageButton allLampUp;
    private ImageButton allLampDown;

    private boolean isContinue = true;

    public LampPopWindow(Context context) {
        super(context);
    }

    private final Handler viewHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    refreshUI();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    public void showPopWindow(View view, int xOffsetToView, int yOffsetToView) {
        contentView = LayoutInflater.from(context).inflate(R.layout.lamp_view, null);
        super.showPopWindow(view, xOffsetToView, yOffsetToView);

        bindAdapter(contentView);

        //防黑边
        this.setBackgroundDrawable(new BitmapDrawable());
        //显示
        GradientDrawable myGrad = (GradientDrawable) contentView.getBackground();
        myGrad.setColor(lampPopBackgroundColor);

        imageBackLamp = (ImageView) contentView.findViewById(R.id.image_back_lamp);
        allLampUp = (ImageButton) contentView.findViewById(R.id.all_lamp_up);
        allLampDown = (ImageButton) contentView.findViewById(R.id.all_lamp_down);

        imageBackLamp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        allLampUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAllLampUp();
            }
        });
        allLampDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAllLampDown();
            }
        });

        isContinue = true;

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isContinue) {
                    viewHandler.sendEmptyMessage(0);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                    }
                }
            }
        }).start();
    }

    @Override
    public void dismiss(){
        super.dismiss();
        isContinue = false;
    }

    /**
     * 每隔500毫秒刷新UI
     * 2016/06/01
     */
    private void refreshUI() {
        getValue("relay");
    }

    public void setAllLampUp() {
        if (com.dragon.www.haier.util.Utils.isFastClick(500)) {
            return;
        }
        NetHelper.sendall(context, String.valueOf(getRoomId()), 1, 1, new NetUtils.NetCallBack<DeviceInfo>() {
            @Override
            public void success(DeviceInfo rspData) {
                if (rspData.getError()== 0) {
                    Log.e("全开", "操作成功");
                } else {
                    String DD = rspData.getDescription();
                    Log.e("错误描述", DD);
                }
            }

            @Override
            public void failed(String msg) {
                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }, DeviceInfo.class);
    }

    public void setAllLampDown() {
        if (com.dragon.www.haier.util.Utils.isFastClick(500)) {
            return;
        }
        NetHelper.sendall(context, String.valueOf(getRoomId()), 1, 0, new NetUtils.NetCallBack<DeviceInfo>() {
                    @Override
                    public void success(DeviceInfo rspData) {
                        if (rspData.getError()== 0) {
                            Log.e("全关", "操作成功");
                        } else {
                            String DD = rspData.getDescription();
                            Log.e("错误描述", DD);
                        }
                    }

                    @Override
                    public void failed(String msg) {
                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                    }
                }, DeviceInfo.class);

    }

    public void bindAdapter(View view) {
        gview = (GridView) view.findViewById(R.id.gview);
        mAdapter = new LampAdapter(context);
        mAdapter.setData(getData(MainActivity.TYPE_LAMP));
        gview.setAdapter(mAdapter);
    }
}
